-- Copyright (C) 2013 wgm.china@gmail.com (王光明)
--vss.db为所有list方法的数据库和数据存储函数
--date:2013-12-11

local strfind = string.find
local strsub  = string.sub
local strbyte = string.byte
local strrep  = string.rep
local strchar = string.char
local strrep  = string.rep
local strlen  = string.len
local type    = type
local pairs   = pairs
local tonumber= tonumber

-------1定义公共组件(开始)---------------
local redis 		= require("resty.redis")
local mysql			= require("resty.mysql")
local ssdb			= require("resty.ssdb")
local mongo     = require("resty.mongol")
local config_table  = ngx.shared.gcs_config  					--定义gcs_config缓存
-------1定义公共组件（结束）----------------


-------2加载配置文件（开始）----------------
-------2加载配置文件（开始）----------------

local mysql_host     = config_table:get("p_mysql_host") or "128.127.110.42"	--取mysql的host
local mysql_port     = config_table:get("p_mysql_port")	or "3306"		--取mysql的port
local mysql_user     = config_table:get("p_mysql_user") or "gcs"		    --取mysql的密码
local mysql_passwd   = config_table:get("p_mysql_passwd") or "gcstorage"	    --取mysql的密码
local mysql_database = config_table:get("p_mysql_database") or "sjky"	    --取mysql的数据库名
local mysql_timout   = config_table:get("p_mysql_timout") or "1000"		--取mysql的连接时间
local mysql_pool_timeout = config_table:get("p_mysql_pool_timeout") or 0  --取mysql的数据库名
local mysql_pool_count   = config_table:get("p_mysql_pool_count") or 100  --取mysql的连接时间

local redis_host   = config_table:get("p_redis_host") or "127.0.0.1"				--取redis的host
local redis_port   = config_table:get("p_redis_port")	or "6379"			--取redis的port
local redis_passwd = config_table:get("p_redis_passwd") or ""			--取redis的密码
local redis_timout = config_table:get("p_redis_timout") or "1000"			--取redis的连接时间
local redis_pool_timeout = config_table:get("p_redis_pool_timeout") or 1000  --取mysql的数据库名
local redis_pool_count   = config_table:get("p_redis_pool_count") or 300  --取mysql的连接时间

local ssdb_host   = config_table:get("p_ssdb_host") or "127.0.0.1"				--取redis的host
local ssdb_port   = config_table:get("p_ssdb_port")	or "8888"			--取redis的port
local ssdb_timout = config_table:get("p_ssdb_timout") or "1000"			--取redis的连接时间
local ssdb_pool_timeout = config_table:get("p_ssdb_pool_timeout") or 0  --取mysql的数据库名
local ssdb_pool_count   = config_table:get("p_ssdb_pool_count") or 100  --取mysql的连接时间


----------------------测试mongol
local mongodb_host = config_table:get("p_mongodb_host") or "127.0.0.1"
local mongodb_port = config_table:get("p_mongodb_port") or "27017"
local mongodb_timout = config_table:get("p_mongodb_timout") or "10000"
local mongodb_database = config_table:get("p_mongodb_database") or "test"
local mongodb_pool_timeout = config_table:get("p_mongodb_pool_timeout") or 1000  --取mysql的数据库名
local mongodb_pool_count   = config_table:get("p_mongodb_pool_count") or 300  --取mysql的连接时间

--------------------


-------2加载配置文件（结束）----------------

module(...)
local version = '1.1'

-------2加载配置文件（结束）----------------
--return version 中保存的版本号
function get_version()
  return version
end

---队列数据库连接
function redis_connect()
  local red,err = redis:new()
  if not red then
      return nil,"redis new nil:"..err
  end
  red:set_timeout(tonumber(redis_timout))
  local ok, err,errno = red:connect(redis_host,redis_port)
  if not ok then
    return nil,"redis connect error:"..err
  end
  if strlen(redis_passwd)>1 then
    red:auth(redis_passwd)
  end
  return red
end
---队列数据库使用连接池
function redis_close(red)
  local timeout = tonumber(redis_pool_timeout) or 0
  local count   = tonumber(redis_pool_count) or 100
  local ok, err = red:set_keepalive(timeout, count)
  if not ok then
    return nil,"redis close error:"..err
  end
  return true
end

---ssdb数据库连接
function ssdb_connect()
  local ssdb_db = ssdb:new()
  ssdb_db:add_commands("setx")
  ssdb_db:set_timeout(tonumber(ssdb_timout))
  local ok, err,errno = ssdb_db:connect(ssdb_host,ssdb_port)
  if not ok then
    return nil,"ssdb connect error:"..err
  end

  return ssdb_db
end
---ssdb数据库使用连接池
function ssdb_close(ssdb_db)
  local timeout = tonumber(ssdb_pool_timeout) or 0
  local count   = tonumber(ssdb_pool_count) or 100
  local ok, err = ssdb_db:set_keepalive(timeout, count)
  if not ok then
    return nil,"redis close error:"..err
  end
  return true
end




function mysql_connection()
  local db = mysql:new()
  db:set_timeout(mysql_timout) -- 1 sec
  local result
  local ok, err, errno, sqlstate = db:connect({
    host = mysql_host,
    port = mysql_port,
    database = mysql_database,
    user = mysql_user,
    password = mysql_passwd,
    max_packet_size=10485760})

  if not ok then
    return nil,"mysql connect error:"..err
  else
    return db
  end
end

function mysql_close(db)
  local timeout = tonumber(mysql_pool_timeout) or 0
  local count   = tonumber(mysql_pool_count) or 100
  local ok, err = db:set_keepalive(timeout, count)
  if not ok then
    return nil,"mysql close connect error:"..err
  else
    return true
  end
end


-------------------------测试mongol--

function mongo_connect()
  local conn = mongo:new()
  conn:set_timeout(mongodb_timout)
  local ok, err = conn:connect(mongodb_host,mongodb_port)
  if not ok then
    return nil,nil,"mongodb connect err:"..err
  end
  local db,err = conn:new_db_handle (mongodb_database)
  if not db then
    return nil,nil,"mongodb connect database err:"..err
  end
  return conn,db,nil
end


function mongo_close(conn)
  local timeout = tonumber(mongodb_pool_timeout) or 0
  local count   = tonumber(mongodb_pool_count) or 100
  local ok,err = conn:set_keepalive(timeout, count)
  if not ok then
    return nil,"mongodb close err:"..err
  end
end